# -*- coding: utf-8 -*-
from tank.core.documents import Document


class Book(Document):
    _name = 'note.book'
    _schema = {
        "type": 'object',
        "properties": {
            "_id": {"type": "ObjectId"},
            "owner": {"type": 'number'},
            "isPublished": {"type": "boolean"},
            "name": {"type": "string"},
        },
        "required": ["owner", "name"]
    }

    @property
    def notes(self):
        return Note.find({'bookId': self.oid, '_delete': False})


class Note(Document):
    _name = 'note.note'
    _schema = {
        "type": 'object',
        "properties": {
            "_id": {"type": "ObjectId"},
            "owner": {"type": 'number'},
            "bookId": {
                "anyOf": [
                    {"type": "ObjectId"},
                    {"type": "null"}
                ]
            },
            "isPublished": {"type": "boolean"},
            "title": {"type": "string"},
            "content": {"type": "string"},

        },
        "required": ["owner", "title", "content", ]
    }

    @property
    def book_id(self):
        return self.get('bookId')

    @property
    def book(self):
        return Book.find_by_oid(self.book_id)
